package com.weiyuwqj.app.notice;

import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.service.BusAccountBankService;
import com.weiyuwqj.api.service.BusAccountCashService;
import com.weiyuwqj.api.service.BusAccountRechargeService;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2017/4/5.
 */

@Controller
public class CallBackNoticeController {

    private static Logger log = Logger.getLogger("CallBackNoticeController");

    @Autowired
    private BusAccountRechargeService accountRechargeService;
    @Autowired
    private BusAccountCashService accountCashService;
    @Autowired
    private BusAccountBankService accountBankService;
    @Autowired
    private BasUserService userService;


    //提现的异步回调
    @ResponseBody
    @RequestMapping(value = "/callback/notice/withdraw", method = RequestMethod.POST)
    public Object withdrawNotice(String plat_no, //平台编号
                                 String platcust, //平台客户号
                                 String order_no, //订单号
                                 String order_amt, //订单金额
                                 String trans_date, //订单日期
                                 String trans_time, //订单时间
                                 String pay_order_no, //提现订单号
                                 String pay_finish_date, //支付完成日期
                                 String order_status, //交易状态 0-申请成功 1-提现成功 2-失败
                                 String pay_amt, //支付金额
                                 String error_info, //失败原因
                                 String error_no, //失败编码
                                 String signdata//签名数据
    ) {
        TreeMap returnMap = new TreeMap();
        returnMap.put("plat_no", plat_no);
        returnMap.put("platcust", platcust);
        returnMap.put("order_no", order_no);
        returnMap.put("order_amt", order_amt);
        returnMap.put("trans_date", trans_date);
        returnMap.put("trans_time", trans_time);
        returnMap.put("pay_order_no", pay_order_no);
        returnMap.put("pay_finish_date", pay_finish_date);
        returnMap.put("order_status", order_status);
        returnMap.put("pay_amt", pay_amt);
        if (error_no == null) {
            returnMap.put("error_no", "");
        } else {
            returnMap.put("error_no", error_no);
            log.info("cash===================error_no=" + error_no);
        }
        if (error_no == null) {
            returnMap.put("error_info", "");
        } else {
            returnMap.put("error_info", error_info);
            log.info("cash===================error_info=" + error_info);
        }
        Map ret = new HashMap();
        log.info("cash===================oderid=" + pay_order_no);
        log.info("cash===================signdata=" + signdata);

        //if(map.get("signdata").equals(signdata)){
        if ("1".equals(order_status)) {
            ret.put("recode", "success");
        } else if ("2".equals(order_status)) {
            accountCashService.updateBusAccountCashStatus(pay_order_no, 2, 1, IpUtils.get());
            ret.put("recode", "success");
        } else if ("3".equals(order_status)) {
            accountCashService.updateBusAccountCashStatus(pay_order_no, 2, 0, IpUtils.get());
            ret.put("recode", "fail");
        }
        return ret;
    }

    //绑卡异步回调
    @ResponseBody
    @RequestMapping(value = "/callback/notice/bindCard", method = RequestMethod.POST)
    public Object bindCard(String plat_no,     //平台编号
                           String platcust,    //平台客户编号
                           String order_no,    //订单号
                           String trans_date,  //托管系统订单日期(YYYY-MM-DD)
                           String trans_time,  //托管系统订单时间(HH:MM:SS)
                           String order_status, //订单状态
                           String error_info,  //失败原因
                           String error_no,    //失败编码
                           String signdata     //签名数据
    ) {
        TreeMap returnMap = new TreeMap();
        returnMap.put("plat_no", plat_no);
        returnMap.put("platcust", platcust);
        returnMap.put("order_no", order_no);
        returnMap.put("trans_date", trans_date);
        returnMap.put("trans_time", trans_time);
        returnMap.put("order_status", order_status);
        if (error_no == null) {
            returnMap.put("error_no", "");
        } else {
            returnMap.put("error_no", error_no);
            log.info("cash===================error_no=" + error_no);
        }
        if (error_no == null) {
            returnMap.put("error_info", "");
        } else {
            returnMap.put("error_info", error_info);
            log.info("cash===================error_info=" + error_info);
        }
        Map ret = new HashMap();
        log.info("cash===================platcust=" + platcust);
        log.info("cash===================signdata=" + signdata);
        //查询用户userId
        BasUser user = userService.getUserByPlatformId(platcust);
        Map map = new HashMap();
        map.put("userId", user.getId());
        map.put("oldStatus", 0);
        if ("1".equals(order_status)) {
            ret.put("recode", "success");
        } else if ("2".equals(order_status)) {
            map.put("newStatus", 1);
            accountBankService.updateBankStatus(map);
            ret.put("recode", "success");
        } else if ("3".equals(order_status)) {
            ret.put("recode", "fail");
        }
        return ret;
    }

    /**
     * 平台充值的异步回调
     *
     * @return 处理状态
     */
    @ResponseBody
    @RequestMapping(value = "/callback/notice/platformRecharge", method = RequestMethod.POST)
    public Object platformRecharge(String plat_no, //平台编号
                                   String order_no, //订单号
                                   String amt, //金额
                                   String code, //1.入账成功 2.入账失败
                                   String signdata //签名数据
    ) {
        TreeMap returnMap = new TreeMap();
        returnMap.put("plat_no", plat_no);
        returnMap.put("order_no", order_no);
        returnMap.put("amt", amt);
        returnMap.put("code", code);

        //Map map = SignUtils.toVerify(returnMap);
        Map ret = new HashMap();
        log.info("cash===================oderid=" + order_no);
        //if(map.get("signdata").equals(signdata)){
        Integer dealStatus;
        if ("1".equals(code)) {
            dealStatus = 1;
        } else {
            dealStatus = 0;
        }
        accountRechargeService.updateBusAccountRechargeStatus(order_no, 2, dealStatus, IpUtils.get());

        ret.put("recode", "success");
        return ret;
    }

    /**
     * 平台提现的异步回调
     *
     * @return 处理状态
     */
    @ResponseBody
    @RequestMapping(value = "/callback/notice/platformWithdraw", method = RequestMethod.POST)
    public Object platformWithdraw(String plat_no, //平台编号
                                   String order_no, //订单号
                                   String amt, //金额
                                   String code, //1.入账成功 2.入账失败
                                   String signdata //签名数据
    ) {
        TreeMap returnMap = new TreeMap();
        returnMap.put("plat_no", plat_no);
        returnMap.put("order_no", order_no);
        returnMap.put("amt", amt);
        returnMap.put("code", code);

        //Map map = SignUtils.toVerify(returnMap);
        Map ret = new HashMap();
        log.info("cash===================oderid=" + order_no);
        //if(map.get("signdata").equals(signdata)){
        Integer dealStatus;
        if ("1".equals(code)) {
            dealStatus = 1;
        } else {
            dealStatus = 0;
        }
        accountCashService.updateBusAccountCashStatus(order_no, 2, dealStatus, IpUtils.get());

        ret.put("recode", "success");
        return ret;
    }

}
